package com.widevision.activity;

import net.widevision.android.R;
import net.widevision.android.TwitterApp;
import net.widevision.android.TwitterApp.TwDialogListener;
import twitter4j.TwitterException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.widevision.ListActivity.UserProfile;

public class Main extends Activity
{
	 LinearLayout Account;
	 LinearLayout notification,friends,albums,profile;
	 int id;
	 String token;
	 TwitterApp mTwitter;
	 private static final String twitter_consumer_key = "JXmXcE5yFPLIRCCtQ79eg";
	 private static final String twitter_secret_key = "dnVojAwaewvEt8E2QwLYtavwm8cdUHwph7lZCaQPMY";
	 
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main1);
		Account=(LinearLayout)findViewById(R.id.lout9);
		notification=(LinearLayout)findViewById(R.id.lout4);
		friends=(LinearLayout)findViewById(R.id.lout5);
		albums=(LinearLayout)findViewById(R.id.lout6);
		profile=(LinearLayout) findViewById(R.id.lout8);
		
		profile.setOnClickListener(new OnClickListener() {
		public void onClick(View v)
		{
				Intent i=new Intent(Main.this,UserProfile.class);
				i.putExtra("USER", "USER");
			    i.putExtra("USERID", id);
				startActivity(i);
		}
		});
		
		Account.setOnClickListener(new OnClickListener() {
		public void onClick(View v)
		{
			onTwitterClick();
		}
		});
		mTwitter 	= new TwitterApp(this, twitter_consumer_key,twitter_secret_key);
		mTwitter.setListener(mTwLoginDialogListener);
		
		if (mTwitter.hasAccessToken())
		{
			String username = mTwitter.getUsername();
			username		= (username.equals("")) ? "Unknown" : username;
			
			try 
			{
				id=mTwitter.getId();
			}
			catch (IllegalStateException e)
			{
				e.printStackTrace();
			}
			catch (TwitterException e) 
			{
				e.printStackTrace();
			}
		}
	}
		
		
	protected void onTwitterClick() 
	{
		if (mTwitter.hasAccessToken()) 
		{
			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			
			builder.setMessage("Delete current Twitter connection?")
			       .setCancelable(false)
			       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			        	   mTwitter.resetAccessToken();
			           }
			       })
			       .setNegativeButton("No", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id)
			           {
			                dialog.cancel();
			           }
			       });
			final AlertDialog alert = builder.create();
			
			alert.show();
		} 
		else
		{
			mTwitter.authorize();
		}
		
	}
	
	private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
		@Override
		public void onComplete(String value) 
		{
			String username = mTwitter.getUsername();
			username		= (username.equals("")) ? "No Name" : username;
			Toast.makeText(Main.this, "Connected to Twitter as " + username, Toast.LENGTH_LONG).show();
		}
		
		@Override
		public void onError(String value) 
		{
			Toast.makeText(Main.this, "Twitter connection failed", Toast.LENGTH_LONG).show();
		}
	};

}
